package com.pudutech.bluetoothdemo

import android.bluetooth.*
import android.content.BroadcastReceiver
import android.content.Context
import android.content.Intent
import androidx.appcompat.app.AppCompatActivity
import android.os.Bundle
import android.util.Log
import android.widget.Toast
import com.pudutech.bluetoothdemo.bluetooth.BluetoothUtils
import kotlinx.android.synthetic.main.activity_main.*
import java.util.*

class MainActivity : AppCompatActivity() {
    var TAG = "MainActivity"



    override fun onCreate(savedInstanceState: Bundle?) {
        super.onCreate(savedInstanceState)
        setContentView(R.layout.activity_main)
        initListener()




    }



    private fun initListener() {
       clientBt.setOnClickListener {
           var intent=Intent(this,ClientActivity::class.java)
           startActivity(intent)
       }

        serverBt.setOnClickListener {
            var intent=Intent(this,ServerActivity::class.java)
            startActivity(intent)
        }

    }


    /**
     * 注册蓝牙连接回调
     */
    private fun pRegist(mac: String) {
        BluetoothUtils.registerBluetoothReceiver(this, object : BroadcastReceiver() {
            override fun onReceive(context: Context, intent: Intent) {
                val action: String? = intent.action
                when (action) {
                    BluetoothDevice.ACTION_FOUND -> { //发现新设备连接
                        val device: BluetoothDevice? =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
                        Log.v(TAG, "------发现新设备连接 ${device?.name}  ${device?.address}")
                        device?.let {
                            if (mac == it.address) {
                                Log.v(TAG, "------找到需连接设备 ${device?.name}  ${device?.address}")
                                //停止扫描
                                BluetoothUtils.getBluetoothAdapter(this@MainActivity)
                                    ?.cancelDiscovery()
                                //蓝牙配对
                                BluetoothUtils.Bonde.createBond(device)
                            }
                        }
                    }
                    BluetoothDevice.ACTION_BOND_STATE_CHANGED -> {
                        val device: BluetoothDevice? =
                            intent.getParcelableExtra(BluetoothDevice.EXTRA_DEVICE)
                        var pairingResult: String? = null
                        when (device?.bondState) {
                            BluetoothDevice.BOND_BONDING -> {
                                //正在配对......
                                pairingResult = "寻找配对   正在配对......"
//                                connectStatusTv?.text = pairingResult
                            }
                            BluetoothDevice.BOND_BONDED -> {
                                //完成配对
                                //startMessageTest(device)
                                pairingResult = "寻找配对   完成配对"
//                                connectStatusTv?.text = pairingResult
                                //当前配对的  device
//                                currentDevice = device
                            }
                            BluetoothDevice.BOND_NONE -> {
                                //取消配对
                                pairingResult = "寻找配对   取消配对"
//                                connectStatusTv?.text = pairingResult
                            }
                        }
                        Log.v(TAG, "------$pairingResult")
                    }
                }
            }
        })
    }


}